import unittest
from alg import Solution  # 假设算法文件为 alg.py，且包含 Solution 类

class TestIslandCount(unittest.TestCase):
    def setUp(self):
        """初始化测试实例"""
        self.solution = Solution()

    def test_example1(self):
        """测试示例1：单个岛屿"""
        grid = [
            ["1", "1", "1", "1", "0"],
            ["1", "1", "0", "1", "0"],
            ["1", "1", "0", "0", "0"],
            ["0", "0", "0", "0", "0"]
        ]
        expected = 1
        result = self.solution.numIslands(grid)
        self.assertEqual(result, expected, f"测试示例1失败：预期{expected}，实际{result}")

    def test_example2(self):
        """测试示例2：3个岛屿"""
        grid = [
            ["1", "1", "0", "0", "0"],
            ["1", "1", "0", "0", "0"],
            ["0", "0", "1", "0", "0"],
            ["0", "0", "0", "1", "1"]
        ]
        expected = 3
        result = self.solution.numIslands(grid)
        self.assertEqual(result, expected, f"测试示例2失败：预期{expected}，实际{result}")

    def test_empty_grid(self):
        """测试空网格"""
        grid = []
        expected = 0
        result = self.solution.numIslands(grid)
        self.assertEqual(result, expected, f"测试空网格失败：预期{expected}，实际{result}")

    def test_all_water(self):
        """测试全水域网格"""
        grid = [
            ["0", "0", "0"],
            ["0", "0", "0"],
            ["0", "0", "0"]
        ]
        expected = 0
        result = self.solution.numIslands(grid)
        self.assertEqual(result, expected, f"测试全水域失败：预期{expected}，实际{result}")

    def test_all_land(self):
        """测试全陆地网格（单个岛屿）"""
        grid = [
            ["1", "1", "1"],
            ["1", "1", "1"],
            ["1", "1", "1"]
        ]
        expected = 1
        result = self.solution.numIslands(grid)
        self.assertEqual(result, expected, f"测试全陆地失败：预期{expected}，实际{result}")

    def test_diagonal_land(self):
        """测试对角陆地（不连通，3个岛屿）"""
        grid = [
            ["1", "0", "1"],
            ["0", "0", "0"],
            ["1", "0", "1"]
        ]
        expected = 3
        result = self.solution.numIslands(grid)
        self.assertEqual(result, expected, f"测试对角陆地失败：预期{expected}，实际{result}")

    def test_edge_islands(self):
        """测试边缘岛屿（2个岛屿）"""
        grid = [
            ["1", "0", "0"],
            ["0", "0", "0"],
            ["0", "0", "1"]
        ]
        expected = 2
        result = self.solution.numIslands(grid)
        self.assertEqual(result, expected, f"测试边缘岛屿失败：预期{expected}，实际{result}")

if __name__ == "__main__":
    unittest.main()